package com.pangolin.pangolinim.service.thread;

import com.orhanobut.logger.Logger;
import com.pangolin.pangolinim.app.BroadcastMsg;
import com.pangolin.pangolinim.service.system.AppRuntime;
import com.pangolin.pangolinim.service.thread.base.baseThread;
import com.pangolin.pangolinim.utils.BundleUtil;

import java.io.IOException;

public class socketThread extends baseThread {
    public static final int CONNECT = 1;        //连接服务器命令
    public static final int DISCONNECT = 2;     //断开连接命令

    private int cmd;

    public socketThread(int cmd){
        this.cmd = cmd;
    }

    @Override
    public void run() {
        switch (cmd){
            case CONNECT:
                connect();
                break;
            case DISCONNECT:
                disconnect();
                break;
            default:
        }
    }

    private void connect(){
        if(AppRuntime.getHost() == null || AppRuntime.getPort() == 0){
            Logger.e("connectThread:run:config error:%s|%d", AppRuntime.getHost() , AppRuntime.getPort());
            //TODO:将错误信息返回给上层UI
            BundleUtil bundleUtil = new BundleUtil(BroadcastMsg.SIG_CORE_SYSERR , "server config missing");
            BroadcastSender(bundleUtil.getBundle());
            return;
        }
        if(mSocket.isconnect()){
            Logger.d("connectThread:run:already connected:%s|%d", AppRuntime.getHost() , AppRuntime.getPort());
            BundleUtil bundleUtil = new BundleUtil(BroadcastMsg.SIG_CORE_CONNECT , "");
            BroadcastSender(bundleUtil.getBundle());
            return;
        }
        Logger.d("connectThread:run:start connect:%s|%d" , AppRuntime.getHost() , AppRuntime.getPort());
        try {
            //TODO:连接服务器
            if (mSocket.connect(AppRuntime.getHost(), AppRuntime.getPort())) {
                BundleUtil bundleUtil = new BundleUtil(BroadcastMsg.SIG_CORE_CONNECT , "");
                BroadcastSender(bundleUtil.getBundle());
                Logger.d("connectThread:run:socket connected");
            } else {
                BundleUtil bundleUtil = new BundleUtil(BroadcastMsg.SIG_CORE_DISCONNECT , "connect failed");
                BroadcastSender(bundleUtil.getBundle());
                Logger.d("connectThread:run:socket error");
            }
        } catch (Exception e) {
            //TODO:将错误信息返回给上层UI
            Logger.e("connectThread:run:error:%s", e.getMessage());
            BundleUtil bundleUtil = new BundleUtil(BroadcastMsg.SIG_CORE_NETERR , e.getMessage());
            BroadcastSender(bundleUtil.getBundle());
        }
    }

    private void disconnect(){
        try{
            mSocket.disconnect();
            //通知UI网络断开
            BundleUtil bundleUtil = new BundleUtil(BroadcastMsg.SIG_CORE_DISCONNECT , "");
            BroadcastSender(bundleUtil.getBundle());
        }catch (IOException e){
            //TODO:将错误信息返回给上层UI
            BundleUtil bundleUtil = new BundleUtil(BroadcastMsg.SIG_CORE_NETERR , e.getMessage());
            BroadcastSender(bundleUtil.getBundle());
        }
    }
}
